package com.study.algorithm.ordinaryArray;

public class maxSubArray {
    //一个整数数组中和最大的连续子数组，kadane算法
    public static int maxSubArray(int[] nums) {
        if (nums == null || nums.length == 0){
            return 0;
        }
        int currentSum=nums[0], maxSum=nums[0]; //初始化当前和和最大和
        for(int i=1; i<nums.length; i++){
            currentSum = Math.max(nums[i], currentSum+nums[i]);
            // 更新最大和
            maxSum = Math.max(maxSum, currentSum);
        }
        return maxSum;
    }

    public static void main(String[] args) {
        int[] nums = {-2,1,-3,4,-1,2,1,-5,4};
        System.out.println(maxSubArray(nums));
    }
}
